package com.oazon.moco.link.ui;

import io.netty.channel.ChannelHandlerContext;

import java.util.Arrays;
import java.util.List;

import android.util.Log;

import com.oazon.moco.lan.core.http.bean.AVTransportUri;
import com.oazon.moco.lan.core.http.bean.ControlVolume;
import com.oazon.moco.lan.core.http.bean.DeleteBean;
import com.oazon.moco.lan.core.http.bean.FavoriteBean;
import com.oazon.moco.lan.core.http.bean.MediaStateInstance;
import com.oazon.moco.lan.core.http.bean.ResponseUriToQueue;
import com.oazon.moco.lan.core.http.bean.Seek;
import com.oazon.moco.lan.core.http.bean.SongInfo;
import com.oazon.moco.lan.core.http.bean.UriToQueue;
import com.oazon.moco.lan.core.http.bean.VolumeInstance;
import com.oazon.moco.link.core.LinkEvent;
import com.oazon.moco.link.core.LinkEventListener;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LanEventSource;
import com.oazon.moco.link.core.source.LinkEventSource;
import com.oazon.moco.link.event.PlayerEvent;
import com.oazon.moco.link.event.PlayerStatus;

public class PlayEventListener extends LinkEventListener {

	private static final String TAG = "PlayEventListener";

	private LinkEventSource source = null;

	public PlayEventListener(LinkEventSource... source) {
		setBlackList(Arrays.asList(source));
	}

	@Override
	public void onLinkEvent(LinkEvent event) {
		source = (LinkEventSource) event.getSource();
		String uuid = null;
		if (filter((LinkEventSource) event.getSource())
				|| !(event instanceof PlayerEvent)) {
			return;
		}

		PlayerEvent playEvent = (PlayerEvent) event;

		if (PlayerStatus.ADD_URI_TO_QUEUE.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}

			ResponseUriToQueue responseUriToQueue = addUriToQueue(uuid,
					playEvent.getUriToQueue(),
					(LinkEventSource) playEvent.getSource());

			if (responseUriToQueue != null) {
				LinkSDK.getInstance().addUriToQueueResponse(
						(LinkEventSource) playEvent.getSource(),
						responseUriToQueue);
			}

		} else if (PlayerStatus.PLAY.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			play(uuid, playEvent.getInstanceID(), playEvent.getSpeed());
		} else

		if (PlayerStatus.PAUSE.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			pause(uuid);
		} else

		if (PlayerStatus.STOP.equals(playEvent.getType())) {
			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			stop(uuid);
		} else

		if (PlayerStatus.FAVORITE.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			if (event.getSource() instanceof LanEventSource) {
				favorite(uuid, playEvent.getFavoriteOrder(),
						((LanEventSource) event.getSource())
								.getChannelContext());
			} else {
				favorite(uuid, playEvent.getFavoriteOrder(), null);
			}
		} else

		if (PlayerStatus.PREVIOUS.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			previous(uuid);
		} else

		if (PlayerStatus.DELETE.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			if (event.getSource() instanceof LanEventSource) {
				delete(uuid, playEvent.getDeleteOrder(),
						((LanEventSource) event.getSource())
								.getChannelContext());
			} else {
				delete(uuid, playEvent.getDeleteOrder(), null);
			}
		} else if (PlayerStatus.NEXT.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			next(uuid);
		} else if (PlayerStatus.PLAY_MODE.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			playMode(uuid, playEvent.getPlayMode());
		} else if (PlayerStatus.PROGRESS.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			setProgress(uuid, playEvent.getSeek());
		} else if (PlayerStatus.VOLUME.equals(playEvent.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			setVolume(uuid, playEvent.getVolumeInstance());
		} else if (PlayerStatus.MEDIA_STATE.equals(playEvent.getType())) {
			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			notifyMediaState(uuid, playEvent.getInstances());
		} else if (PlayerStatus.VOLUME_UP.equals(playEvent.getType())) {
			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			setVolumeUp(uuid);
		} else if (PlayerStatus.VOLUME_DOWN.equals(playEvent.getType())) {
			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			setVolumeDown(uuid);
		} else

		if (PlayerStatus.SHIFT.equals(playEvent.getType())) {
			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			setPlayShift(uuid);
		} else if (PlayerStatus.SET_AV_TRANSPORT_URI
				.equals(playEvent.getType())) {
			if (event.getSource() instanceof LanEventSource) {
				uuid = ((LanEventSource) event.getSource()).getUuid();
			} else {
				uuid = LinkSDK.UUID;
			}
			setAVTransportUri(uuid, playEvent.getAvTransportUri());

		} else if (PlayerStatus.ADD_URI_TO_QUEUE_RESPONSE.equals(playEvent
				.getType())) {

			if (event.getSource() instanceof LanEventSource) {
				if (playEvent.getResponseUriToQueue() != null) {
					responseAddUriToQueue(playEvent.getResponseUriToQueue(),
							((LanEventSource) event.getSource())
									.getChannelContext());
				}
			}
		}
	}

	protected void responseAddUriToQueue(ResponseUriToQueue responseUriToQueue,
			ChannelHandlerContext channelContext) {

	}

	protected void setAVTransportUri(String uuid, AVTransportUri avTransportUri) {

	}

	protected ResponseUriToQueue addUriToQueue(String uuid,
			UriToQueue uriToQueue, LinkEventSource linkEventSource) {

		return null;
	}

	protected void sendVolume(String uuid, VolumeInstance volumeInstance) {

	}

	/**
	 * 播放状态切换，如果暂停就播放，如果播放就暂停
	 * 
	 * @param uuid
	 */
	protected void setPlayShift(String uuid) {

	}

	public LinkEventSource getEventSource() {
		return source;
	}

	/**
	 * 减音量每次减1
	 * 
	 * @param uuid
	 *            uuid是局域网协议的参数
	 */
	protected void setVolumeDown(String uuid) {

	}

	/**
	 * 加音量每次加1
	 * 
	 * @param uuid
	 *            uuid是局域网协议的参数
	 */
	protected void setVolumeUp(String uuid) {

	}

	/**
	 * 切歌的时候响应的事件，用于向控制设备发送歌曲信息，UI不需要理会
	 * 
	 * @param uuid
	 * @param songInfo
	 */
	protected void notifyMediaState(String uuid, MediaStateInstance instances) {
		try {
			Log.d(TAG, "切歌====");
		} catch (Exception e) {
		}

	}

	/**
	 * 播放上一首歌
	 * 
	 * @param uuid
	 */
	protected void previous(String uuid) {
		Log.d(TAG, "上一首====");
	}

	/**
	 * 改变音量
	 * 
	 * @param uuid
	 * @param volumeInstance
	 *            音量大小【0~31】
	 */
	protected void setVolume(String uuid, VolumeInstance volumeInstance) {
		Log.d(TAG, "音量====" + volumeInstance);

	}

	/**
	 * 改变播放的进度
	 * 
	 * @param uuid
	 * @param seek
	 *            毫秒
	 */
	protected void setProgress(String uuid, Seek seek) {
		Log.d(TAG, "进度设置到====" + seek);

	}

	/**
	 * 设置播放模式
	 * 
	 * @param uuid
	 * @param playMode
	 *            见类com.oazon.moco.bean.PlayMode
	 */
	protected void playMode(String uuid, String playMode) {
		Log.d(TAG, "播放模式====" + playMode);

	}

	/**
	 * 播放下一首歌
	 * 
	 * @param uuid
	 */
	protected void next(String uuid) {
		Log.d(TAG, "下一首");

	}

	/**
	 * 删除歌曲
	 * 
	 * @param uuid
	 * @param deleteBean
	 *            包含了歌曲id和是否删除
	 */
	protected void delete(String uuid, DeleteBean deleteBean,
			ChannelHandlerContext ctx) {
		Log.d(TAG,
				"删除" + deleteBean.getCurrentItemID() + ":"
						+ deleteBean.isDelete());

	}

	/**
	 * 喜欢收藏
	 * 
	 * @param uuid
	 * @param favoriteBean
	 *            包含了歌曲id和是否喜欢
	 */
	protected void favorite(String uuid, FavoriteBean favoriteBean,
			ChannelHandlerContext ctx) {
		// Log.d(TAG, "喜欢" + favoriteBean.getCurrentItemID() + ":" +
		// favoriteBean.isLike());
	}

	/**
	 * 停止播放
	 * 
	 * @param uuid
	 */
	protected void stop(String uuid) {
		Log.d(TAG, "停止");

	}

	/**
	 * 暂停播放
	 * 
	 * @param uuid
	 */
	protected void pause(String uuid) {
		Log.d(TAG, "暂停");

	}

	/**
	 * 继续播放
	 * 
	 * @param uuid
	 * @param speed
	 * @param instanceID
	 */
	protected void play(String uuid, int instanceID, int speed) {
		Log.d(TAG, "播放");

	}
}
